package com.taptech.doufu.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.util.DensityUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.util.ScreenUtil;

public class TopTabHostViewGroup extends ViewGroup implements OnClickListener
{

    protected Context mContext;

    protected int mTabsCount = 0;
    // 屏幕宽度
    protected int mScreenWidth = 768;
    // 标签宽高
    protected int mTabsWidth = 0;
    protected int mTabsHeight = 0;
    protected int mTopPadding = 10;
    // 滑动条高度
    protected int mTabStripWidth = 200;
    // 滑动条高度
    protected int mTabStripHeight = ScreenUtil2.dip2px(3);

    protected int mStripCurrentX = 0;
    protected int mStripCurrentPositon = 0;
    //滑动条速度
    protected final int STRIP_MOVE_DURATION = 300;
    //下方分隔线的高度
    protected final int DILIVER_LINE_HEIGHT = 1;
    //颜色

    protected int TEXT_NORMAL_COLOR = Color.parseColor("#ff8f8f8f");
    protected int TEXT_CHOOSE_COLOR = Color.parseColor("#ffff6e70");

    protected int TEXT_NORMAL_COLOR_DARK = WeMediaApplication.getInstance().getResources().getColor(R.color.text_dark);
    protected int TEXT_CHOOSE_COLOR_DARK = WeMediaApplication.getInstance().getResources().getColor(R.color.theme_main_dark);
    // 滑动条的颜色
    protected int mStripColor = TEXT_CHOOSE_COLOR;
    // 翻页接口
    protected ChoosePage choosePageListener;
    // 样式
    protected int type = -1;
    private int TEXT_MARGIN_LEFT = 0;
    private int TEXT_MARGIN_TOP = 0;

    public TopTabHostViewGroup(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        initViewGroup(context);
    }

    public TopTabHostViewGroup(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initViewGroup(context);

    }

    public TopTabHostViewGroup(Context context)
    {
        super(context);
        initViewGroup(context);
    }

    protected void initViewGroup(Context context)
    {
        this.mContext = context;
        // 获取屏幕宽度
        mScreenWidth = ScreenUtil.getScreenWidth(mContext);
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            mStripColor = getResources().getColor(R.color.theme_main_dark);
        }

    }

    public void setType(int type)
    {
        this.type = type;
        TEXT_NORMAL_COLOR = Color.parseColor("#ff8f8f8f");
        TEXT_CHOOSE_COLOR = Color.parseColor("#ffffff");
        mTabStripHeight = 0;
        TEXT_MARGIN_LEFT = 20;
        TEXT_MARGIN_TOP = 20;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int parentHeight = getMeasuredHeight();
        // 测量标签
        if (mTabsCount > 0)
        {
            for (int i = 0; i < mTabsCount; i++)
            {
                measureChild(getChildAt(i), mScreenWidth / mTabsCount, parentHeight);
            }
            // 测量顶部条
            if (mTabStripWidth > 0)
            {
                measureChild(getChildAt(mTabsCount), mTabStripWidth, mTabStripHeight);
            } else
            {
                measureChild(getChildAt(mTabsCount), mScreenWidth / mTabsCount, mTabStripHeight);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        if (mTabsCount > 0)
        {
            for (int i = 0; i < mTabsCount; i++)
            {
                View childView = getChildAt(i);
                int childL = getMeasuredWidth() * i / mTabsCount;
                int childR = childL + getMeasuredWidth() / mTabsCount;
                childView.layout(childL, (getMeasuredHeight() - childView.getMeasuredHeight()) / 2, childR, (getMeasuredHeight() + childView.getMeasuredHeight()) / 2);
            }
            // 放在标签栏下面

            getChildAt(mTabsCount).layout(0, this.getMeasuredHeight() - getChildAt(mTabsCount).getMeasuredHeight(), mScreenWidth / mTabsCount, this.getMeasuredHeight());

            if (getChildCount() > 0)
            {
                getChildAt(getChildCount() - 1).layout(0, this.getMeasuredHeight() - DILIVER_LINE_HEIGHT, mScreenWidth, this.getMeasuredHeight());
            }

            //TTLog.d("tag", "tttt" + (this.getMeasuredHeight() - getChildAt(mTabsCount).getMeasuredHeight()));
        }

    }

    /**
     * 输入字符串数组成生顶部标签栏
     *
     * @param strs
     */
    public void putStringsTabs(String[] strs)
    {
        if (strs != null)
        {
            // 新建标签
            mTabsCount = strs.length;
            for (int i = 0; i < mTabsCount; i++)
            {

                Log.d("tag", "组件高度为" + getMeasuredHeight());
                TextView textView = new TextView(mContext);
                LinearLayout.LayoutParams params;
                if (type == 1)
                {
                    params = new LinearLayout.LayoutParams(mScreenWidth / mTabsCount, 80);
                    mTopPadding = 10;
                    // textView.setPadding(mTopPadding, mTopPadding, mTopPadding, mTopPadding);
                    // params.setMargins(10, 10, 10, 10);
                } else
                {
                    params = new LinearLayout.LayoutParams(mScreenWidth / mTabsCount, LayoutParams.MATCH_PARENT);
                    mTopPadding = 0;
                    textView.setPadding(0, mTopPadding, 0, 0);
                }
                textView.setLayoutParams(params);
                textView.setTextSize(14);
                textView.setText(strs[i]);
                if (i == 0)
                {
                    textView.setTextColor(TEXT_CHOOSE_COLOR);
                    if (type == 1)
                    {
                        textView.setBackgroundColor(Color.parseColor("#ff9295"));
                        //textView.setBackgroundResource(R.drawable);
                    }
                } else
                {
                    textView.setTextColor(TEXT_NORMAL_COLOR);
                    if (type == 1)
                    {
                        textView.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                }
                textView.setGravity(Gravity.CENTER);
                textView.setTag(i);
                textView.setOnClickListener(this);
                addView(textView);
            }

            LinearLayout lineLayout=new LinearLayout(mContext);
//            lineLayout.setBackgroundColor(Color.WHITE);
            lineLayout.setGravity(Gravity.CENTER_HORIZONTAL);
            lineLayout.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lineParams=new LinearLayout.LayoutParams(mScreenWidth / mTabsCount, mTabStripHeight);
            lineLayout.setLayoutParams(lineParams);

            ImageView tabStrip = new ImageView(mContext);
            LayoutParams params = new LayoutParams(DensityUtil.sp2px(mContext,14*2), mTabStripHeight);
            tabStrip.setLayoutParams(params);
            tabStrip.setBackgroundColor(mStripColor);

            lineLayout.addView(tabStrip);
            addView(lineLayout);

            //底部分隔线
            ImageView diliverLineView = new ImageView(mContext);
            LayoutParams diliverLine_params = new LayoutParams(LayoutParams.MATCH_PARENT, DILIVER_LINE_HEIGHT);
            diliverLineView.setLayoutParams(diliverLine_params);
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                diliverLineView.setBackgroundColor(getResources().getColor(R.color.sep_dark));
            }else {
                diliverLineView.setBackgroundColor(Color.parseColor("#ffe3e3e3"));
            }

            addView(diliverLineView);
        }
    }

    /**
     * 将滑动条移动到相应位置下
     *
     * @param position
     */
    public void moveTopStripTo(final int position)
    {
        if (mStripCurrentX != mScreenWidth * position / mTabsCount)
        {

            Animation animation = new TranslateAnimation(mStripCurrentX, mScreenWidth * position / mTabsCount, 0, 0);
            animation.setDuration(STRIP_MOVE_DURATION);
            animation.setFillAfter(true);
            animation.setAnimationListener(new AnimationListener()
            {

                @Override
                public void onAnimationStart(Animation animation)
                {
                    changeTabTextColor(position, TEXT_CHOOSE_COLOR, TEXT_NORMAL_COLOR);
                }

                @Override
                public void onAnimationRepeat(Animation animation)
                {

                }

                @Override
                public void onAnimationEnd(Animation animation)
                {

                }
            });
            getChildAt(mTabsCount).startAnimation(animation);

            mStripCurrentX = mScreenWidth * position / mTabsCount;
        }

    }

    /**
     * 改变顶部栏文字颜色
     *
     * @param position
     * @param chooseColor   被选中时颜色
     * @param noChooseColor 未被选中的颜色
     */
    protected void changeTabTextColor(int position, int chooseColor, int noChooseColor)
    {
        TextView tabTextView = (TextView) getChildAt(mStripCurrentPositon);
        tabTextView.setTextColor(noChooseColor);
        tabTextView = (TextView) getChildAt(position);
        tabTextView.setTextColor(chooseColor);
        mStripCurrentPositon = position;
        if (type == 1)
        {
            for (int i = 0; i < mTabsCount; i++)
            {
                TextView textView = (TextView) getChildAt(i);
                if (i == mStripCurrentPositon)
                {
                    textView.setTextColor(chooseColor);
                    textView.setBackgroundColor(Color.parseColor("#ff9295"));
                } else
                {
                    textView.setTextColor(noChooseColor);
                    textView.setBackgroundColor(Color.parseColor("#ffffff"));
                }
            }
        }
    }

    public interface ChoosePage
    {
        public void updatePage(int position);
    }

    public void setChoosePageListener(ChoosePage choosePageListener)
    {
        this.choosePageListener = choosePageListener;
    }

    @Override
    public void onClick(View v)
    {
        moveTopStripTo((Integer) v.getTag());
        choosePageListener.updatePage((Integer) v.getTag());
    }

    public int getmTabsWidth()
    {
        return mTabsWidth;
    }

    public void setmTabsWidth(int mTabsWidth)
    {
        this.mTabsWidth = mTabsWidth;
    }

    public int getmTabsHeight()
    {
        return mTabsHeight;
    }

    public void setmTabsHeight(int mTabsHeight)
    {
        this.mTabsHeight = mTabsHeight;
    }

    public int getmStripColor()
    {
        return mStripColor;
    }

    public void setmStripColor(int mStripColor)
    {
        this.mStripColor = mStripColor;
    }

    public void setmTabStripHeight(int mTabStripHeight)
    {
        // 滑动条高度
        this.mTabStripHeight = 0;
    }
}
